package body v8.Class_String is

   function Create (S : Standard.String) return Pointer is
   begin
      if S'Length = 0 then
         return Empty;
      else
         declare
            function Invoke
              (Data   : Standard.String := S;
               Length : Integer_32 := S'Length
              ) return Pointer;
            pragma Import (CPP, Invoke, "_ZN2v86String3NewEPKci");
         begin
            return Invoke;
         end;
      end if;
   end Create;
   pragma Inline (Create);

   function Create (S : Wide_String) return Pointer is
   begin
      if S'Length = 0 then
         return Empty;
      else
         declare
            function Invoke
              (Data   : Wide_String := S;
               Length : Integer_32 := S'Length
              ) return Pointer;
            pragma Import (CPP, Invoke, "_ZN2v86String3NewEPKti");
         begin
            return Invoke;
         end;
      end if;
   end Create;
   pragma Inline (Create);


   function New_Undetectable (S : Standard.String) return Pointer is
   begin
      if S'Length = 0 then
         return Empty;
      else
         declare
            function Invoke
              (Data      : Standard.String := S;
               Length    : Integer_32 := S'Length
              ) return Pointer;
            pragma Import (CPP, Invoke, "_ZN2v86String15NewUndetectableEPKci");
         begin
            return Invoke;
         end;
      end if;
   end New_Undetectable;
   pragma Inline (New_Undetectable);

   function New_Undetectable_Utf16 (S : Wide_String) return Pointer is
   begin
      if S'Length = 0 then
         return Empty;
      else
         declare
            function Invoke
              (Data   : Wide_String := S;
               Length : Integer_32 := S'Length
              ) return Pointer;
            pragma Import (CPP, Invoke, "_ZN2v86String15NewUndetectableEPKti");
         begin
            return Invoke;
         end;
      end if;
   end New_Undetectable_Utf16;
   pragma Inline (New_Undetectable_Utf16);

   function New_Symbol (S : Standard.String) return Pointer is
      function Invoke
        (data   : Standard.String;
         length : int := S'Length
        ) return Pointer;
      pragma Import (CPP, Invoke, "_ZN2v86String9NewSymbolEPKci");
   begin
      return Invoke (S);
   end New_Symbol;
   pragma Inline (New_Symbol);

   function Slice
     (this   :  v8.Class_String.String;
      From   : Positive := 1;
      Length : Integer := -1
     ) return Standard.Wide_String
   is
   begin
      if From + Length > Integer (this.Length) then
         return "";
      else
         declare
            subtype Result_Type is Wide_String (1 .. (if Length = -1 then Integer (this.Length) else Length));
            Result : aliased Result_Type;
            function Copy
              (Src    :  v8.Class_String.String;
               buffer : access Result_Type := Result'Access;
               start  : Integer := From - 1;
               size   : Integer := Length;
               hint   : String_Write_Hints.Enum := String_Write_Hints.NO_HINTS
              ) return Integer;
            pragma Import (CPP, Copy, "_ZNK2v86String5WriteEPtiiNS0_10WriteHintsE");

            N : Integer := Copy (this);
         begin
            return Result;
         end;
      end if;
   end Slice;
end v8.Class_String;
